package com.czq.zk2.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czq.zk2.entity.Weekstudent;
import com.czq.zk2.service.WeekstudentService;
import com.czq.zk2.struct.PageResult;
import com.czq.zk2.struct.Result;
import com.czq.zk2.util.StringUtils;
import com.czq.zk2.vo.SearchVONew;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 关注公众号：czq
 * @since 2022-05-30
 */
@RestController
@RequestMapping("/weekstudent")
public class WeekstudentController {

    private final static Logger logger= LoggerFactory.getLogger(WeekstudentController.class);
    @Resource
    WeekstudentService weekstudentService;
   @PostMapping("searchzk/{currentpage}/{pagesize}")
    public PageResult<Weekstudent> searchzk(@PathVariable("currentpage") Integer currentpage,
                                            @PathVariable("pagesize") Integer pagesize,
                                            @RequestBody(required = false)SearchVONew vo)
   {
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
       QueryWrapper<Weekstudent> queryWrapper = new QueryWrapper<>();
       Page<Weekstudent> tPage = new Page<>();
       if(Objects.nonNull(vo))
       {
           if(!StringUtils.isEmpty(vo.getUsername()))
           {
               queryWrapper.like("name",vo.getUsername());
           }
           if(!StringUtils.isEmpty(vo.getDatebegin())&&!StringUtils.isEmpty(vo.getDateend()))
           {
               queryWrapper.between("createdt",vo.getDatebegin().substring(0,10),vo.getDateend().substring(0,10));
           }
       }

       Page<Weekstudent> page = this.weekstudentService.page(tPage, queryWrapper);
       long total = page.getTotal();
       PageResult<Weekstudent> of = PageResult.of(page.getRecords(), total);

       return of;
   }

   @PostMapping("addAccount1")
    public Result addAccount1(@RequestBody Weekstudent taccount)
   {
       String substring =taccount.getCreatedt().substring(0,10);
       taccount.setCreatedt(substring);
       boolean save = this.weekstudentService.save(taccount);
       logger.info("插入数据成功，值是："+save);
       return save?Result.ok():Result.error();
   }

   @PostMapping("deleteAll")
    public Result deleteAll(@RequestBody List<Integer> ids)
   {
       boolean save = this.weekstudentService.removeByIds(ids);
       return save?Result.ok():Result.error();
   }

    @PostMapping("delete/{id}")
    public Result delete(@PathVariable Integer id)
    {
        boolean save = this.weekstudentService.removeById(id);
        logger.info("删除数据为："+save);
        return save?Result.ok():Result.error();
    }

    @PutMapping("/putwk")
    public Result update(@RequestBody Weekstudent userRole)
    {
        return this.weekstudentService.saveOrUpdate(userRole)?Result.ok():Result.error();
    }


}
